<?php
#http://www.php.net/manual/en/function.session-set-save-handler.php

# the following php should alredy beed 'required' by CGIApplication.class.php
#require_once('site_config.php');
#require_once('commonUtil.php');

class SessionHandler_Memcache {
  
  public $lifeTime;
  public $memcache;

  function __construct() {
    global $site_global_config;    
  	
  	$memcache_pool = get_hash_param($site_global_config, 'SESSION_MEMCACHE', ''); # check if not exists
	  
	  $this->memcache = new Memcache;
    foreach ($memcache_pool as $server){
				list ($host, $port) = explode(':',$server);
				#echo("memcache $host, $port\n");
		    $this->memcache->connect($host, $port); # or error ... pls implement	 
		}
    #return $this->memcache->connect('localhost', 11211) or header("Location: /maintenance.php?error=100");
    
    
  }
  
  
	# this function should not be called from outside, 
	# as it is use the use site_config setting
  function connect_memcache_server($a_memcache_server){
		# when to reconnect ?
		
		if (! isset($this->memcache_obj)){
			
	  }
	  
 }
  

  function open($savePath, $sessName) {
    return $this->lifeTime = get_cfg_var("session.gc_maxlifetime");
  }

  function close() {
    return $this->gc(ini_get('session.gc_maxlifetime'));
  }

  function read($sessID) {
      return $this->memcache->get($sessID);
  }

  function write($sessID,$sessData) {
      return $this->memcache->set($sessID, $sessData, false, $this->lifeTime) or die ("Failed to save data at the server");
  }

  function destroy($sessID) {
      return $this->memcache->delete($sessID);
  }

  function gc($sessMaxLifeTime) {
  }

}

#$session = new session();
#session_set_save_handler(array(&$session,"open"),
#                       array(&$session,"close"),
#                       array(&$session,"read"),
#                       array(&$session,"write"),
#                       array(&$session,"destroy"),
#                       array(&$session,"gc"));
#
#session_start();  

?>